package main

import (
	"context"
	"fmt"
	"sync"
	"time"
)

var ctx = context.Background()

func f2(ctx context.Context, group *sync.WaitGroup) chan int {
	defer group.Done()
	var ch = make(chan int, 6)
	n := 1
LOOP:
	for {
		fmt.Println("	f2")
		time.Sleep(time.Millisecond * 500)
		select {
		case <-ctx.Done():
			fmt.Println(ctx.Err())
			fmt.Println(ctx.Deadline())
			break LOOP
		case ch <- n:
			n++
		}
	}
	//for v := range ch {
	//	fmt.Println(v)
	//}
	return ch
}
func f1(ctx context.Context, group *sync.WaitGroup) chan int {
	//defer group.Done()
	go f2(ctx, group)
	var ch = make(chan int, 6)
	n := 1
LOOP:
	for {
		fmt.Println("f1")
		time.Sleep(time.Millisecond * 500)
		select {
		case <-ctx.Done():
			fmt.Println(ctx.Err())
			break LOOP
		case ch <- n:
			n++
		}
	}
	//for v := range ch {
	//	fmt.Println(v)
	//}
	return ch
}
func main() {
	ctxWithCancel, cancel := context.WithCancel(ctx)
	var wg sync.WaitGroup
	wg.Add(1)
	go f1(ctxWithCancel, &wg)
	time.Sleep(time.Second * 1)
	cancel()
	wg.Wait()
}
